package com.mq.mqtest.service.impl;

import com.mq.mqtest.entity.Order;
import com.mq.mqtest.entity.User;
import com.mq.mqtest.service.IUserService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @author peng.li
 * @date 2021/6/14 18:45
 */
@Service
@Slf4j
@RabbitListener(queues = {"boot-queue1", "order.release.order.queue"}) //监听release队列
public class UserServiceImpl implements IUserService {

    @RabbitHandler
    public void listener(Order order, Channel Channel, Message message) throws Exception {
        System.out.println(LocalDateTime.now().toString() + "点收到一条过期未支付的订单消息：订单：" + order);
        System.out.println("*******该订单已关闭**********");
        //签收
        Channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
}

    @RabbitHandler
    public void receiveMsg(User user, Channel channel, Message message) {
        log.warn("收到消息user:" + user + "，内容：" + user);
        log.info("user消息：" + user.getName() + "处理完成");
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            //参数
            // deliveryTag：当前消息的“派发的标签”
            //boolean：false:只签收当前处理的这条消息。不批量签收所有的
            channel.basicAck(deliveryTag, false);

            //拒收收货方法参数依次为：
            //deliveryTag
            //multiple：是否批量拒收。默认false，如果改为true那么以前手动回复的确认也全部被拒
            //            requeue：被拒绝的消息要不要重新回到队列。true重新入队，false则该消息被废弃掉
            //            channel.basicNack(deliveryTag, false, true);
        } catch (IOException e) {
            //网络中断，签收完成的状态发不出去了
            e.printStackTrace();
        }
    }

//    /*******测试监听两种内容类型的消息********/
//    @RabbitHandler
//    public void receive2Msg(Order order) {
//        log.warn("收到消息order:" + order + "，内容：" + order);
//        log.info("order消息：" + order.getUserAddress() + "处理完成");
//
//    }
}
